const FocusDetailModule = require('../../modules/recommend/focusDetail')
const CosmeticsDetail = require('../../modules/base/cosmeticsDetail')
const UserInfo = require('../../modules/user')

const qs = require('querystring')

class FocusDetailControllers {
  /**
   * 添加关注
   */
  static async addFocus(ctx) {
    const req = ctx.request.body
    if (!req.userId || !req.detailId) {
      ctx.body = {
        code: 416,
        msg: '缺少参数'
      }
      return
    }
    try {
      let userExist = (await UserInfo.getAllUserList({
        id: req.userId
      })).length
      let detailExist = (await CosmeticsDetail.getCosmeticsDetail({
        detail_id: req.detailId
      })).length

      if (userExist && detailExist) {
        await FocusDetailModule.setFocus({detailId: req.detailId, userId: req.userId})
        ctx.body = {
          code: 200,
          msg: 'success'
        }
      } else {
        ctx.body = {
          code: 412,
          msg: '请求的资源不存在'
        }
      }
    } catch (err) {
      console.log(err)
      ctx.body = {
        code: 400,
        msg: err.stack
      }
    }
  }

  /**
   * 取消关注
   */
  static async unFocus(ctx) {
    const req = ctx.request.body
    if (!req.userId || !req.detailId) {
      ctx.body = {
        code: 416,
        msg: '缺少参数'
      }
      return
    }
    try {
      FocusDetailModule.unFocus({
        userId: req.userId,
        detailId: req.detailId
      })
      ctx.body = {
        code: 200,
        msg: 'success'
      }
    } catch (err) {
      console.log(err)
      ctx.body = {
        code: 400,
        mas: err.stack
      }
    }
  }

  /**
   * 查询关注
   */
  static async getFocus(ctx) {
    const req = qs.parse(ctx.request.url.split('?')[1])
    if (!req.userId) {
      ctx.body = {
        code: 416,
        msg: '缺少参数'
      }
      return
    }
    let where = {}
    if (req.detailId) {
      where = {
        detailId: req.detailId,
        userId: req.userId
      }
    } else {
      where = {
        userId: req.userId
      }
    }
    try {
      let focus = await FocusDetailModule.getFocus(where)
      ctx.body = {
        code: 200,
        data: focus
      }
    } catch (err) {
      console.log(err)
      ctx.body = {
        code: 400,
        msg: err.stack
      }
    }
  }

  /**
   * 时间线
   */
  static async focusTimeline(ctx) {
    const req = qs.parse(ctx.request.url.split('?')[1])
    if (!req.userId) {
      ctx.body = {
        code: 416,
        msg: '缺少参数'
      }
      return
    }

    try {
      let focusList = await FocusDetailModule.getFocus({userId: req.userId})
      let data = []
      for(var i = 0; i < focusList.length; i++) {
        await (function(){
          return new Promise(function(res, rej){
            (async () => {
              // let l = await ClassifyFieldMap.getMap({content: list[i].content})
              // data.push({
              //   classify: list[i].cosmeticsClassify,
              //   content: list[i].content,
              //   list: l
              // })
              let item = await CosmeticsDetail.getCosmeticsDetailItem({detail_id: focusList[i].detailId})
              data.push({
                timestamp: focusList[i].createAt,
                detail: item
              })
              res()
            })()
          })
        }())
      }
      ctx.body = {
        code: 200,
        data
      }
    } catch (err) {
      console.log(err)
    }
  }
}

module.exports = FocusDetailControllers
